library(survey)
library(dplyr)
library(ggplot2)
library(gridExtra)

# Creating Figure 6.4

# Wave 2
summary(factor(df$Q36))
summary(factor(df$P_ASSIGN5))

# Create binary for vote for Trump (2)
summary(factor(df$Q36))
df$Q36T[df$Q36 < 77] <- 0
df$Q36T[df$Q36 == 2] <- 1
summary(factor(df$Q36T))

# Create new variables for assigned condition
df2 <- df

# Mil biden
df2$PA5_mbd[df2$P_ASSIGN5 == 1 & df2$party == 0] <- 0
df2$PA5_mbd[df2$P_ASSIGN5 == 2 & df2$party == 0] <- 1
df2$PA5_mbi[df2$P_ASSIGN5 == 1 & df2$party == 1] <- 0
df2$PA5_mbi[df2$P_ASSIGN5 == 2 & df2$party == 1] <- 1
df2$PA5_mbr[df2$P_ASSIGN5 == 1 & df2$party == 2] <- 0
df2$PA5_mbr[df2$P_ASSIGN5 == 2 & df2$party == 2] <- 1

# Mil trump
df2$PA5_mtd[df2$P_ASSIGN5 == 1 & df2$party == 0] <- 0
df2$PA5_mtd[df2$P_ASSIGN5 == 3 & df2$party == 0] <- 1
df2$PA5_mti[df2$P_ASSIGN5 == 1 & df2$party == 1] <- 0
df2$PA5_mti[df2$P_ASSIGN5 == 3 & df2$party == 1] <- 1
df2$PA5_mtr[df2$P_ASSIGN5 == 1 & df2$party == 2] <- 0
df2$PA5_mtr[df2$P_ASSIGN5 == 3 & df2$party == 2] <- 1

# Gen Biden
df2$PA5_gbd[df2$P_ASSIGN5 == 1 & df2$party == 0] <- 0
df2$PA5_gbd[df2$P_ASSIGN5 == 4 & df2$party == 0] <- 1
df2$PA5_gbi[df2$P_ASSIGN5 == 1 & df2$party == 1] <- 0
df2$PA5_gbi[df2$P_ASSIGN5 == 4 & df2$party == 1] <- 1
df2$PA5_gbr[df2$P_ASSIGN5 == 1 & df2$party == 2] <- 0
df2$PA5_gbr[df2$P_ASSIGN5 == 4 & df2$party == 2] <- 1

# Gen Trump
df2$PA5_gtd[df2$P_ASSIGN5 == 1 & df2$party == 0] <- 0
df2$PA5_gtd[df2$P_ASSIGN5 == 5 & df2$party == 0] <- 1
df2$PA5_gti[df2$P_ASSIGN5 == 1 & df2$party == 1] <- 0
df2$PA5_gti[df2$P_ASSIGN5 == 5 & df2$party == 1] <- 1
df2$PA5_gtr[df2$P_ASSIGN5 == 1 & df2$party == 2] <- 0
df2$PA5_gtr[df2$P_ASSIGN5 == 5 & df2$party == 2] <- 1


w2_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df2
  )


# Wave 2
# P_ASSIGN5 == 2 BIDEN
w2mbd <- svyttest(Q36T~PA5_mbd, w2_design)
w2mbi <- svyttest(Q36T~PA5_mbi, w2_design)
w2mbr <- svyttest(Q36T~PA5_mbr, w2_design)

# P_ASSIGN5 == 3 TRUMP
w2mtd <- svyttest(Q36T~PA5_mtd, w2_design)
w2mti <- svyttest(Q36T~PA5_mti, w2_design)
w2mtr <- svyttest(Q36T~PA5_mtr, w2_design)

# P_ASSIGN5 == 4 Generals BIDEN
w2gbd <- svyttest(Q36T~PA5_gbd, w2_design)
w2gbi <- svyttest(Q36T~PA5_gbi, w2_design)
w2gbr <- svyttest(Q36T~PA5_gbr, w2_design)

# P_ASSIGN5 == 5 Generals TRUMP
w2gtd <- svyttest(Q36T~PA5_gtd, w2_design)
w2gti <- svyttest(Q36T~PA5_gti, w2_design)
w2gtr <- svyttest(Q36T~PA5_gtr, w2_design)

# Wave 2
pt.est2 <- c(w2mbd$estimate, w2mbi$estimate, w2mbr$estimate, 
             w2mtd$estimate, w2mti$estimate, w2mtr$estimate, 
             w2gbd$estimate, w2gbi$estimate, w2gbr$estimate, 
             w2gtd$estimate, w2gti$estimate, w2gtr$estimate)
ci.low2 <- c(w2mbd$conf.int[1], w2mbi$conf.int[1], w2mbr$conf.int[1], 
             w2mtd$conf.int[1], w2mti$conf.int[1], w2mtr$conf.int[1], 
             w2gbd$conf.int[1], w2gbi$conf.int[1], w2gbr$conf.int[1], 
             w2gtd$conf.int[1], w2gti$conf.int[1], w2gtr$conf.int[1])
ci.high2 <- c(w2mbd$conf.int[2], w2mbi$conf.int[2], w2mbr$conf.int[2], 
              w2mtd$conf.int[2], w2mti$conf.int[2], w2mtr$conf.int[2], 
              w2gbd$conf.int[2], w2gbi$conf.int[2], w2gbr$conf.int[2], 
              w2gtd$conf.int[2], w2gti$conf.int[2], w2gtr$conf.int[2])
plot.labs <- c('','Military:\nBiden',' ',
               '  ','Military:\nTrump','   ',
               '    ','Generals:\nBiden','     ',
               '      ','Generals:\nTrump','       ')
df.plot2 <- data.frame(Treatment=plot.labs, estimate=pt.est2, 
                       lower=ci.low2, upper=ci.high2)
df.plot2$Treatment <- factor(df.plot2$Treatment, levels = plot.labs)

plot_w2 <- ggplot()+
  geom_pointrange(data=df.plot2, 
                  mapping = aes(x=Treatment,
                                y=estimate,
                                ymin=lower,
                                ymax=upper),
                  size = 0.5, color = c('blue','gray50','red',
                                        'blue','gray50','red',
                                        'blue','gray50','red',
                                        'blue','gray50','red'))+
  geom_hline(yintercept = 0, color = "black")+
  ylim(-.275,.175)+
  ylab("Difference in Means from Control Condition")+
  xlab("Treatment Condition")+
  ggtitle(label="Wave 2")+
  theme_bw()


